//
//  WCJPresentationMessage.swift
//  jackWu
//
//  Created by Lewhaming on 2016/11/23.
//  Copyright © 2016年 IQ. All rights reserved.
//

import UIKit

let PresentationManagerDidPresented = "PresentationManagerDidPresented"
let PresentationManagetDidDismissed = "PresentationManagerDissmissed"


class WCJPresentationMessage: NSObject , UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    private var isPresent = false
    var presentFrame = CGRect()
    
    //该方法用于放回一个负责转场动画的对象
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        
        let pc = WCJPresentationController(presentedViewController: presented, presenting: presenting)
        
        pc.presentFrame = presentFrame
        
        return pc
    }
    //改方法负责如何消失对象
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        NotificationCenter.default.post(name: NSNotification.Name(PresentationManagetDidDismissed), object: self)
        return self;
    }
    
    //该方法用于负责如何出现对象
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        //发送一个通知,告诉调用者状态发生改变
        NotificationCenter.default.post(name: NSNotification.Name(PresentationManagerDidPresented), object: self)
        return self;
    }
    
    //告诉动画时长
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    
    //管理如何展现和消失
    //如果实现了这个方法，就需要自己去添加了
    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        if  isPresent{
           willPresentedController(using: transitionContext)
        }else{
           willDismissededController(using: transitionContext)
        }
    }
    
    
    //执行展现
    private func willPresentedController(using transitionContext: UIViewControllerContextTransitioning){
        guard let toVc = transitionContext.view(forKey: UITransitionContextViewKey.to)
            else{
                return
        }
        
        
        IQLog(message: toVc)
        
        toVc.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        transitionContext.containerView.addSubview(toVc)
        toVc.transform = CGAffineTransform(scaleX: 1.0, y: 0)
        UIView.animate(withDuration:transitionDuration(using: transitionContext), animations: {
            () -> Void in
            toVc.transform = CGAffineTransform.identity
        }){(_)->Void in
            transitionContext.completeTransition(true)
        }
    }
    //执行消失
    private func willDismissededController(using transitionContext: UIViewControllerContextTransitioning)
    {
        guard let FromVc = transitionContext.view(forKey: UITransitionContextViewKey.from)
            else{
                return
        }
        //        FromVc.transform = CGAffineTransform(scaleX: 1.0, y: 0.0001)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            () -> Void in
            FromVc.transform = CGAffineTransform(scaleX: 1.0, y: 0.0001)
            //                FromVc.transform = CGAffineTransform.identity
        }){(_)->Void in
            transitionContext.completeTransition(true)
        }
    }
}

